<template>
	<view class="container">		
		<uni-section title="资产信息" type="line">
			<uni-list>
				<uni-list-item title="分类型号" :rightText="form.cate_name + ' ' + form.model_name"/>
				<uni-list-item title="资产编号" :rightText="form.num"/>
				<uni-list-item title="型号参数" :rightText="form.params"/>
				<uni-list-item title="使用情况" :rightText="form.dept_name + ' ' + (form.user_name ? form.user_name : '')"/>
				<uni-list-item title="当前状态" :rightText="dictLabel(statusOptions, form.status)"/>
				<uni-list-item title="归属仓库" :rightText="form.house_name"/>
				<uni-list-item title="资产价值" :rightText="form.price + ''"/>
			</uni-list>
			<view class="uni-list list-pd" v-if="imageList.length > 0">
				<view class="uni-list-cell cell-pd">
					<view class="uni-uploader">
						<view class="uni-uploader-body">
							<view class="uni-uploader__files">
								<block v-for="(image,index) in imageList" :key="index">
									<view class="uni-uploader__file">
										<image class="uni-uploader__img" :src="image" :data-src="image" 
											@tap="previewImage"></image>
									</view>
								</block>
							</view>
						</view>
					</view>
				</view>
			</view>
		</uni-section>
		
		<view class="button-sp-area">
			<button class="mini-btn" type="primary" size="mini" v-if="form.status == '2'" @click="doClick('revert')">归还</button>
			<button class="mini-btn" type="danger" size="mini" v-if="form.status == '1'" @click="doClick('use')">借用</button>
			<button class="mini-btn" type="danger" size="mini" v-if="form.status == '1'" @click="doClick('maintain')">维保</button>
			<button class="mini-btn" type="info" size="mini" @click="doClick('stock')">盘点</button>
		</view>
	</view>
</template>

<script>
	import { selectDictLabel, parseTime, getDicts	} from '@/api/sys.js'
	import { apiGet	} from '@/api/openapi'

	export default {
		data() {
			return {
				id: null,
				num: null,
				statusOptions: [],
				form: {},
				imageList:[],
				params: {
					"@alias":"t",
					id: null,
					num: null,
				}
			}
		},
		onLoad(option) {
			if(option.id){
				this.params.id = option.id;
			}
			if(option.num){
				this.params.num = option.num;
			}			
			this.getData();
			getDicts('assets_status').then(response => {
				response.data.forEach(e=>{
					e.value = e.dictValue;
					e.text = e.dictLabel;
				})
				this.statusOptions = response.data;	
			});
		},
		methods: {
			getData(){
				let params = {
					"asset_info": {...this.params},
					"asset_model": {"@alias":"m", "@column":"name:model_name,params","@join":"m.id = t.model_id"},
					"asset_category":{"@alias":"c","@column":"name:cate_name","@join":"c.id = m.cate_id"},
					"sys_dept":{"@alias":"d","@column":"dept_name","@join":"d.dept_id = t.dept_id"},
					"sys_user":{"@alias":"u","@column":"nick_name:user_name","@join":"LEFT u.user_id = t.user_id"},
					"asset_warehouse":{"@alias":"h","@column":"name:house_name","@join":"LEFT h.id = t.house_id"}
				};
				apiGet(params).then(response => {
					this.form = response.data;
					if(response.data.imgs){
						this.imageList = response.data.imgs.split(',');
					}
				})
			},
			dictLabel(dictData, dictVal) {
				return selectDictLabel(dictData, dictVal);
			},
			doClick(type) {
				if(type == 'stock'){
					uni.redirectTo({
						url: '/pages/asset/stock/asset?assetId='+this.form.id
					});
				}else if(type == 'use'){
					uni.redirectTo({
						url: '/pages/asset/out/use?assetId='+this.form.id
					});
				}else if(type == 'maintain'){
					uni.redirectTo({
						url: '/pages/asset/apply/maintain?assetId='+this.form.id
					});
				}else if(type == 'revert'){
					uni.redirectTo({
						url: '/pages/asset/in/use?assetId='+this.form.id
					});
				}else{
					this.$modal.msgError("不支持的操作");
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>